<?php

/**
 * Initialize the system
 */
define('TL_MODE', 'FE');
require('system/initialize.php');

class HtmlShippingAddress extends Controller
{
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();		
	}
	
	public function compile()
	{
		// import database support.
		$this->import('Database');
		/** countries get **/						 
		$arr_countries = array();
		
		$this->loadLanguageFile('countries');
		include(TL_ROOT . '/system/config/countries.php');
		$obj_countries = $this->Database->prepare("SELECT * FROM `tl_countries` ORDER BY order_num,name")
										->execute(time());
		while ($obj_countries->next())
		{
			if ($obj_countries->order_num != 999)
			{
				$arr_countries[$obj_countries->name] = $countries[$obj_countries->name];
				unset($countries[$obj_countries->name]);
			}
		}
		asort($countries);
		$countries = array_merge($arr_countries, $countries);
		$currentCountry = "";
		$currentStates = "";
		$currentCity = "";
		
		// all states and city
		// Get the states.
		$arrStates_all = array();
		$objStates = $this->Database->prepare("SELECT * FROM `china_province` ORDER BY `province_en`")
									->execute(time());
		while ($objStates->next())
		{
			$arrStates_all[$objStates->id] = stripslashes($objStates->province_en);
		}
		
		// Get State.
		$arrCitys_all = array();			
		$objCitys = $this->Database->prepare("SELECT * FROM `china_city` ORDER BY `sortOrder`,`city_en`")
									->execute(time());
		
		while ($objCitys->next())
		{
			$arrCitys_all[$objCitys->id] = stripslashes($objCitys->city_en);
		}
			
		if (!isset($_GET['id']) || !$_GET['id'])
		{		
			$form_action = "add";
			$id_addr = 0;
			// Get the states.
			$arrStates = array();
			$objStates = $this->Database->prepare("SELECT * FROM `china_province` ORDER BY `province_en`")
										->execute(time());
			
			while ($objStates->next())
			{
				$arrStates[$objStates->id] = stripslashes($objStates->province_en);
			}
			
			// Get State.
			$arrCitys = array();			
			$objCitys = $this->Database->prepare("SELECT * FROM `china_city` ORDER BY `sortOrder`,`city_en`")
										->execute(time());
			
			while ($objCitys->next())
			{
				$arrCitys[$objCitys->id] = stripslashes($objCitys->city_en);
			}
		}
		else
		{
			$form_action = "edit";
			$id_addr = trim($_GET['id']);
			$objAddressbook = $this->Database->prepare("SELECT * FROM `tl_address_book` WHERE `id`=?")
											->execute(trim($_GET['id']));
			
			$firstName = $objAddressbook->firstname;
			$lastName = $objAddressbook->lastname;
			$postcode = $objAddressbook->postcode;
			$telephone = $objAddressbook->telephone;
			$cellphone = $objAddressbook->cellphone;
			$street = $objAddressbook->street;
			$street_2 = $objAddressbook->street_2;
			$currentCountry = $objAddressbook->country;
			$currentStates = $objAddressbook->state;
			$currentCity = $objAddressbook->city;
			if ($objAddressbook->country == 'cn')
			{
				// Get the states.
				$arrStates = array();
				$objStates = $this->Database->prepare("SELECT * FROM `china_province` WHERE 1")
											->execute(time(), time());
				
				while ($objStates->next())
				{
					$arrStates[$objStates->id] = stripslashes($objStates->province_en);
				}
											
				$objCities = $this->Database->prepare("SELECT * FROM `china_city` WHERE `pid`=?")
											->execute($objAddressbook->state);
				$arrCitys = array();
				while ($objCities->next())
				{
					$arrCitys[$objCities->id] = stripslashes($objCities->city_en);
				}
			}
		}
		
		include(TL_ROOT . "/shipping_address.html.php");
		$strBuffer = ob_get_contents();
		ob_end_clean();
		
		echo $strBuffer;exit;		
	}
}

$htmlShippingAddress = new HtmlShippingAddress();
$htmlShippingAddress->compile();

?>